package com.creditall.organization.utils;

import com.alibaba.fastjson.JSON;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 
 * @Date: 
 * @Version 1.0
 */
public class HttpUtils {

    /**
     * post请求以json数据传输
     * @param url
     * @param json
     * @return
     */
    public static String doPostJson(String url, String json) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(resultString);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }

    public static void main(String[] args) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("serial_no","ZX201906132542");
        paramMap.put("merchants_no","3123231232");
        paramMap.put("product_name","房抵产品1号");
        paramMap.put("customer_name","张三");
        paramMap.put("cust_id","420000199202111223");
        paramMap.put("phone_no","17765432345");
        paramMap.put("contract_limit","36");
        paramMap.put("contract_money","180000.00");
        paramMap.put("contarct_rates","8.6");
        //合同模板上需要绑定的参数值（key:pdf模板的表单参数值  value:填充值）
        Map<String,Object> contarctMap = new HashMap<String,Object>();
        contarctMap.put("param1","1");
        contarctMap.put("param2","2");
        contarctMap.put("param3","3");
        contarctMap.put("param4","4");
        contarctMap.put("param5","5");
        contarctMap.put("param6","6");
        paramMap.put("contract_data",contarctMap);

        String paramJson= JSON.toJSONString(paramMap);
        System.out.println("asdfasd"+paramJson);
//        doPostJson("http://211.95.45.30:21680/generateContract",paramJson);
        doPostJson("http://10.10.0.138:8080/generateContract",paramJson);

    }
}
